package com.ruoyi.wms.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.core.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.web.domain.BaseEntity;

/**
 * 工序管理对象 wms_process
 *
 * @author ruoyi
 * @date 2025-10-16
 */
@Data
@ToString
@ApiModel(value = "工序管理对象", description = "工序管理对象")
public class Process {
    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    private String id;

    /**
     * 工序编码
     */
    @ApiModelProperty(value = "工序编码")
    private String processCode;

    /**
     * 工序顺序
     */
    @ApiModelProperty(value = "工序顺序")
    private String processSequence;

    /**
     * 工序名称
     */
    @ApiModelProperty(value = "工序名称")
    private String processName;

    /**
     * 作业方式
     */
    @ApiModelProperty(value = "作业方式")
    private String operationMethod;

    /**
     * 拆解物
     */
    @ApiModelProperty(value = "拆解物")
    private String dismantlingMaterials;

    @ApiModelProperty(value = "拆解物二维数组")
    private  String[][]  dismantlingMaterialsArr;
    /**
     * 管理方式
     */
    @ApiModelProperty(value = "管理方式")
    private String management;

    /**
     * 单位
     */
    @ApiModelProperty(value = "单位")
    private String unit;

    /**
     * 是否单码管理
     */
    @ApiModelProperty(value = "是否单码管理")
    private Long singleCode;

    /**
     * 状态
     */
    @ApiModelProperty(value = "原材料状态")
    private String status;

    /**
     * 拆解员
     */
    @ApiModelProperty(value = "拆解员")
    private String dismantler;

    @ApiModelProperty(value = "拆解员")
    private String[] dismantlerArr;

    /**
     * 录入人
     */
    @ApiModelProperty(value = "录入人")
    private String createUser;

    /**
     * 录入日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    private String createDate;

    /**
     * 修改时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    private String updateDate;

    /**
     * 数据来源
     */
    @ApiModelProperty(value = "数据来源")
    private String dataSource;

    /** 质检项item */
    @Excel(name = "质检项item")
    @ApiModelProperty(value = "质检项item")
    private String qualityInspectionItem;

    public String[] getdismantlerArr() {
        if (dismantlerArr != null && dismantlerArr.length > 0) {
            // 数组不为空且包含元素
            return dismantlerArr;
        }

        if (StringUtils.isNotEmpty(dismantler)) {
            return dismantler.split(",");
        }
        return dismantlerArr;
    }
    public String getdismantler() {
         if (dismantlerArr == null || dismantlerArr.length == 0) {

            if( this.dismantler !=null && !this.dismantler.contains(",")){
                if(dismantler.contains("|")){
                    this.dismantler =this.dismantler.replace("|",",");
                }
                return  this.dismantler;
            }
            return null;
        }

        if( this.dismantler !=null && !this.dismantler.contains(",")){
            return  this.dismantler;
        }

        return String.join(",", dismantlerArr);
    }



    // 重写getter方法 - 从二维数组生成字符串
    public String getDismantlingMaterials() {
        if (dismantlingMaterialsArr == null) {
            return null;
        }

        List<String> allMaterials = new ArrayList<>();
        for (String[] materialGroup : dismantlingMaterialsArr) {
            if (materialGroup != null && materialGroup.length > 0) {
                // 将每组材料用逗号连接，然后用分号分隔不同组
                String groupStr = String.join(",", materialGroup);
                allMaterials.add(groupStr);
            }
        }

        return allMaterials.isEmpty() ? null : String.join(";", allMaterials);
    }

    // 重写setter方法确保数据同步
    public void setDismantlingMaterialsArr(String[][] dismantlingMaterialsArr) {
        this.dismantlingMaterialsArr = dismantlingMaterialsArr;
        // 同时更新字符串字段
        this.dismantlingMaterials = getDismantlingMaterials();
    }

    public void setDismantlingMaterials(String dismantlingMaterials) {
        this.dismantlingMaterials = dismantlingMaterials;
        // 同时更新数组字段
        this.dismantlingMaterialsArr = getDismantlingMaterialsArr();
    }

    // 字符串转回二维数组
    public String[][] getDismantlingMaterialsArr() {
        if (dismantlingMaterialsArr != null) {
            return dismantlingMaterialsArr;
        }

        if (dismantlingMaterials != null && !dismantlingMaterials.isEmpty()) {
            String[] groups = dismantlingMaterials.split(";", -1); // 使用-1保留空字符串
            String[][] result = new String[groups.length][];

            for (int i = 0; i < groups.length; i++) {
                if (!groups[i].isEmpty()) {
                    result[i] = groups[i].split("[,|]", -1); // 使用字符类，更简洁

//                    result[i] = groups[i].split(",", -1); // 使用-1保留空字符串
                } else {
                    result[i] = new String[0];
                }
            }
            return result;
        }
        return null;
    }
}
